package com.managementemployees.fragments;

import java.util.ArrayList;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.managementemployees.activities.R;
import com.managementemployees.activities.SaveNhanVienActivity;
import com.managementemployees.adapter.NhanVienAdapter;
import com.managementemployees.database.NhanVienHelper;
import com.managementemployees.model.NhanVien;

public class FragmentNhanVien extends Fragment {
	private ListView mListView;
	private ArrayList<NhanVien> mArrayList = null;
	private NhanVienHelper mNhanVienHelper;
	private NhanVienAdapter mAdapter;
	private Context mContext;
	private View mView;
	private View mViewNoNhanVien;
	private Button btnOk;
	private Button btnCancel;

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		mView = inflater.inflate(R.layout.fragment_nhanvien, container, false);
		mViewNoNhanVien = inflater.inflate(R.layout.no_item_nhanvien, null);
		loadNhanVien(mView);

		return mView;
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
	}

	void loadNhanVien(View view) {
		mContext = getActivity().getApplicationContext();
		mNhanVienHelper = new NhanVienHelper(getActivity());
		mArrayList = mNhanVienHelper.getAllNhanVien();
		if (mArrayList.size() > 0) {
			mListView = (ListView) view.findViewById(R.id.lvNhanVien);
			mAdapter = new NhanVienAdapter(mArrayList, mContext);
			mListView.setAdapter(mAdapter);
			Log.d("DD", "HungND " + mArrayList.size());
			mListView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> adapter, View view,
						int position, long l) {
					// TODO Auto-generated method stub
					Intent i = new Intent(getActivity(),
							SaveNhanVienActivity.class);
					i.putExtra("IdNhanVien", mArrayList.get(position).getId());
					startActivity(i);
				}
			});
			mListView.setOnItemLongClickListener(new OnItemLongClickListener() {

				@Override
				public boolean onItemLongClick(AdapterView<?> adapter,
						View view, int position, long l) {
					// TODO Auto-generated method stub
					delNhanVien(position);
					return false;
				}
			});

		} else {
			// TODO show no item nhan vien
			mView = mViewNoNhanVien;
		}
	}

	void delNhanVien(final int position) {
		final Dialog dialog = new Dialog(getActivity());
		dialog.setContentView(R.layout.custom_dialog_del_nv);
		dialog.setTitle("Xoa Nhan Vien");
		dialog.show();
		btnOk = (Button) dialog.findViewById(R.id.btnOK);
		btnCancel = (Button) dialog.findViewById(R.id.btnHuy);
		btnOk.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mNhanVienHelper.deleteNhanVien(mArrayList.get(position));
				mArrayList.remove(position);
				mAdapter.notifyDataSetChanged();
				if (mArrayList.size() == 0) {
					mView = mViewNoNhanVien;
				}
				dialog.cancel();
			}
		});
		btnCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dialog.cancel();
			}
		});

	}
}
